package com.m2dl.tpcsa.touchmycircle;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Accueil extends Activity {

	/** The file persistent path. */
	protected final String PREFS_SCORE = "file";
	/** Attributes classes. */
	protected Button playGameButton, exitGameButton;
	/** The textView for the score. */
	protected TextView newScoreTextView = null, bestScoreTextView = null;
	/** The string builder score. */
	protected StringBuilder stringBuilderScore = null;
	/** The current score of the application. */
	protected String currentScore;
	/** The manager of the score. */
	SharedPreferences pref = null;

	/** Gets the persistentScore. */
	@SuppressLint("WorldReadableFiles")
	private void showThePersistentScore() {
		// Gets and Shows the persistent score
		this.pref = this.getSharedPreferences(PREFS_SCORE, MODE_WORLD_READABLE);
		String bestScore = " "+ this.pref.getInt("score", 0);
		String newScore = " "+ this.pref.getInt("newScore", 0);
		this.bestScoreTextView.setText(bestScore);
		this.newScoreTextView.setText(newScore);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_accueil);

		// Manages the playGameButton
		this.playGameButton = (Button) findViewById(R.id.accueil_bouton_jouer);
		this.playGameButton.setVisibility(View.VISIBLE);

		// Manages the playGameButton
		this.exitGameButton = (Button) findViewById(R.id.accueil_bouton_quitter);
		this.exitGameButton.setVisibility(View.VISIBLE);

		// Gets and Shows the persistent score
		this.bestScoreTextView = (TextView) findViewById(R.id.accueil_theScore);
		this.newScoreTextView = (TextView) findViewById(R.id.accueil_theNewScore);

		this.showThePersistentScore();
		
		this.playGameButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Accueil.this, TMCGame.class);
				startActivity(intent);
			}
		});
		this.exitGameButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	public void onStart() {
		super.onStart();
		// Gets the persistent score
		this.showThePersistentScore();
	}

	@Override
	public void onStop() {
		super.onStop();
		// this.finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_accueil, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.accueil_quitter:
			finish();
			return true;
		case R.id.accueil_pageScore:
			Toast.makeText(this, "Score menu not present", Toast.LENGTH_LONG)
					.show();
			return true;
		}
		return false;
	}
}
